package gui;

import com.sun.awt.AWTUtilities;
import implementation.NewButton;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import gui.UI;
import implementation.NewButton2;
import implementation.NewLabel;
import variable.Counter;

public class Gui {
    public static void gui(){
        //启动主页面

        GraphicsDevice graphDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        DisplayMode screenSize = graphDevice.getDisplayMode();
        //int width = disMode.getWidth();
        //int height = disMode.getHeight();

        //Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        JFrame frame = new JFrame("Empty-Chat");
        frame.setSize((int)(screenSize.getWidth()/1.5),(int)(screenSize.getHeight()/1.5));
        frame.setLocationRelativeTo(null);
        //frame.setLocation((int)(screenSize.getWidth()/5.5),(int)(screenSize.getHeight()/5.5));
        //frame.setLayout();
        frame.setUndecorated(true);
        //frame.setResizable(true);
        AWTUtilities.setWindowShape(frame, new RoundRectangle2D.Double(
                0.0D, 0.0D, frame.getWidth(), frame.getHeight(), 30.0D,
                30.0D));
        frame.setIconImage(UI.LOGO.getImage());
        frame.setVisible(true);


        //设置组件
        NewLabel label1 = new NewLabel("列表","宋体",18);
        NewLabel label2 = new NewLabel("联系人","宋体",18);
        NewLabel label3 = new NewLabel("Empty-Chat","宋体",20);

        JTextArea textArea1 = new JTextArea(18,80);//信息框
        JTextArea textArea2 = new JTextArea(8,80);//输入框
        JList list = new JList();
        //JTextArea textArea3 = new JTextArea(35,20);//消息框

        textArea1.setBackground(UI.BACKGROUND3);
        textArea1.setForeground(UI.FONT_COLOR);
        textArea1.setFont(UI.FONT);
        textArea1.setEditable(false);
        textArea2.setBackground(UI.BACKGROUND3);
        textArea2.setForeground(UI.FONT_COLOR);
        textArea2.setFont(UI.FONT);

        list.setBackground(UI.BACKGROUND3);
        list.setPreferredSize(new Dimension(250,580));

        //设置ui大小
        UI.FILE1.setImage(UI.FILE1.getImage().getScaledInstance(30,30,Image.SCALE_DEFAULT));
        UI.FILE2.setImage(UI.FILE2.getImage().getScaledInstance(30,30,Image.SCALE_DEFAULT));
        UI.PHOTO1.setImage(UI.PHOTO1.getImage().getScaledInstance(30,30,Image.SCALE_DEFAULT));
        UI.PHOTO2.setImage(UI.PHOTO2.getImage().getScaledInstance(30,30,Image.SCALE_DEFAULT));
        UI.ENTER1.setImage(UI.ENTER1.getImage().getScaledInstance(30,30,Image.SCALE_DEFAULT));
        UI.ENTER2.setImage(UI.ENTER2.getImage().getScaledInstance(30,30,Image.SCALE_DEFAULT));
        UI.DEFAULT1.setImage(UI.DEFAULT1.getImage().getScaledInstance(30,30,Image.SCALE_DEFAULT));
        UI.DEFAULT2.setImage(UI.DEFAULT2.getImage().getScaledInstance(30,30,Image.SCALE_DEFAULT));
        UI.CLOSE.setImage(UI.CLOSE.getImage().getScaledInstance(20,20,Image.SCALE_DEFAULT));
        UI.CLOSE_C.setImage(UI.CLOSE_C.getImage().getScaledInstance(20,20,Image.SCALE_DEFAULT));
        UI.CLOSE_S.setImage(UI.CLOSE_S.getImage().getScaledInstance(20,20,Image.SCALE_DEFAULT));

        NewButton2 button1 = new NewButton2(UI.FILE2,UI.FILE1,new Dimension(30, 30));
        NewButton2 button2 = new NewButton2(UI.PHOTO2,UI.PHOTO1,new Dimension(30, 30));
        NewButton2 button3 = new NewButton2(UI.ENTER2,UI.ENTER1,new Dimension(30, 30));
        NewButton2 button4 = new NewButton2(UI.CLOSE_S,UI.CLOSE,new Dimension(20, 20));
        NewButton2 button5 = new NewButton2(UI.CLOSE_S,UI.CLOSE,new Dimension(20, 20));
        NewButton2 button6 = new NewButton2(UI.CLOSE_C,UI.CLOSE,new Dimension(20, 20));



        //设置面板
        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JPanel panel3 = new JPanel();
        JPanel panel4 = new JPanel();
        JPanel panel5 = new JPanel();
        JScrollPane scrollPane1 = new JScrollPane();
        JScrollPane JScrollPane2 = new JScrollPane();
        Box box1 = Box.createVerticalBox();
        Box box2 = Box.createVerticalBox();

        scrollPane1.setOpaque(true);

        panel1.setVisible(true);
        panel2.setVisible(true);
        panel3.setVisible(true);
        panel4.setVisible(true);
        panel5.setVisible(true);
        panel5.setLayout(new FlowLayout());

        //信息栏
        panel4.setBackground(UI.BACKGROUND2);
        panel4.add(Box.createHorizontalStrut(10));
        panel4.add(label1);
        panel4.add(Box.createHorizontalStrut(600));
        panel4.add(label2);
        panel4.add(Box.createHorizontalStrut(300));

        //主面板
        scrollPane1.setViewportView(textArea1);

        panel2.setBackground(UI.BACKGROUND);
        panel2.add(list);

        panel3.setBackground(UI.BACKGROUND);
        panel3.add(button1);
        panel3.add(button2);
        panel3.add(Box.createHorizontalStrut(800));
        panel3.add(button3);

        box1.add(scrollPane1);
        box1.add(panel3);
        box1.add(textArea2);

        panel1.setBackground(UI.BACKGROUND2);
        panel1.add(panel2);
        panel1.add(Box.createHorizontalStrut(50));
        panel1.add(box1);

        //顶栏
        panel5.setBackground(UI.BACKGROUND);
        panel5.add(label3);
        panel5.add(Box.createHorizontalStrut(frame.getWidth()-200));
        panel5.add(button4);
        panel5.add(button5);
        panel5.add(button6);


        //外层box
        box2.add(panel5);
        box2.add(panel4);
        box2.add(panel1);



        //测试
        frame.add(box2);

        //功能部分
        button1.addActionListener(e -> {});
        button2.addActionListener(e -> {});
        button3.addActionListener(e -> {
            String msg1;
            LocalDateTime dateTime = LocalDateTime.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd HH:mm:ss");
            String data = dateTime.format(formatter);

            msg1 = textArea2.getText();
            textArea1.append(data + "\n" + "> 测试用户:" + "\n" + msg1);
            textArea1.append("\n\n");
            textArea2.setText("");

        });
        button4.addActionListener(e -> {frame.setExtendedState(JFrame.ICONIFIED);});//最小化
        button5.addActionListener(e -> {
            if(frame.getExtendedState() != frame.MAXIMIZED_BOTH) {
                frame.setExtendedState(frame.MAXIMIZED_BOTH);
                AWTUtilities.setWindowShape(frame, new RoundRectangle2D.Double(
                        0.0D, 0.0D, frame.getWidth(), frame.getHeight(), 0.0D,
                        0.0D));
            }else {
                AWTUtilities.setWindowShape(frame, new RoundRectangle2D.Double(
                        0.0D, 0.0D, frame.getWidth(), frame.getHeight(), 30.0D,
                        30.0D));
                frame.setExtendedState(frame.NORMAL);
            }
        });//最大化
        button6.addActionListener(e -> System.exit(0));



    }

}
